package com.eascs.archive.controller.v0;


import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.alibaba.dubbo.config.annotation.Reference;
import com.eascs.admin.common.controller.BaseMasterViewController;
import com.eascs.admin.common.vo.ModelResult;
import com.eascs.app.dao.PageResult;
import com.eascs.app.runtime.RuntimeSetting;
import com.eascs.archive.dubbo.ArchiveTask;
import com.eascs.archive.entity.ArchiveLoanEntity;
import com.eascs.archive.service.ArchiveAdminService;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.page.SelectDataOption;
import com.eascs.site.view.ViewContext;
import com.eascs.site.view.button.AjaxButton;
import com.eascs.site.view.button.Button;
import com.eascs.site.view.button.ButtonHelper;
import com.eascs.site.view.button.ShowButtonFilter;
import com.eascs.workflow.common.service.dictionary.DicDataService;
import com.eascs.workflow.common.service.wf.WorkFlowService;

@RequestMapping("/archive")
@Controller(value="archiveController")
public class ArchiveController extends BaseMasterViewController {
	final static int PAGE_SIZE = 15;

	@Autowired
	private ArchiveTask archiveTask;
	@Autowired
	private ArchiveAdminService archiveAdminService;
	@Reference
	private WorkFlowService workFlowService;
	@Reference
	private DicDataService dicDataService;
	@Autowired
	private com.eascs.archive.service.ArchiveExportService archiveExportService;
	
	@RequestMapping("/queryList")
	public String queryArchiveList(ArchiveLoanEntity param) {
		if (param.getPageSize() == null  || param.getPageSize() == 0) {
			param.setPageSize(PAGE_SIZE);
		}
		ModelResult<PageResult<Object[]>> modelResult=archiveAdminService.queryListPage(param, param.getPageSize(), param.getPageIndex());
		if (modelResult.isSuccess()) {
			PageResult<Object[]> pageResult= modelResult.getModel();
			String[] titles = { "贷款单号", "平台", "信贷经理", "借款人姓名", "放款银行", "放款日期	", "归档完成时间", "存柜档案编号","归档状态"};
			SelectDataOption[] selectDataOptions = { null, null, null, null,new SelectDataOption("BANKTYPEO2O"), null, null, null,new SelectDataOption("PLACEONFILE")};
			PaginationInfo paginationInfo = new PaginationInfo(pageResult, titles, "/archive/v0/queryList?pageIndex=");
			Button start = new AjaxButton("查看", "/archive/rest/v0/archiveHQPopup?referId=") ;
			start.setShowButtonFilter(new ShowButtonFilter() {
				@Override
				public boolean filter(Object param) {
					Object[] params = (Object[]) param;
					String bankName=params[5].toString();
					String state=params[9].toString();
					
					if(StringUtils.isBlank(state) || StringUtils.isBlank(bankName))
						return false;
					
					if (state.equals("CLO") && (StringUtils.equals(bankName, "BC") || StringUtils.equals(bankName, "CBC")
							|| StringUtils.equals(bankName, "GACZ"))) 
						return true;
					return false;
				}
			});
			Button[] titleButton = new Button[] { start };
			Button tBtn = ButtonHelper.createOpenButton(null, "/detail?billCode=");
			tBtn.setShowButtonFilter(new ShowButtonFilter() {
				@Override
				public boolean filter(Object param) {
					Object[] params = (Object[]) param;
					tBtn.setUrl("/detail?billCode=" + params[1]+"&v=");
					
					return true;
				}
			});
			Button[] titleButtons = new Button[]{tBtn};
			paginationInfo.setTitleButtons(titleButtons);
			paginationInfo.setBuildPagingNavigation(true);
			paginationInfo.setFirstRenderColumnIndex(1);
			paginationInfo.setMoreButtons(titleButton);
			paginationInfo.setSelectDataOptions(selectDataOptions);
			ViewContext.put("paginationInfo", paginationInfo);
			ViewContext.put("archiveLoan", param);
		}
		return "/archive/v0/archiveAdminList";
	}
	

	/**开发环境，模拟定时调用*/
	@RequestMapping(value = "/test/schedular", method = RequestMethod.GET)
	public void archiveHQ() {
		RuntimeSetting runtimeSetting = RuntimeSetting.get();
		if (!runtimeSetting.getEnv().equals("prod")) { // 非生产环境
			archiveTask.startUpPlaceOnFile();
		}
	}
}
